package com.asom.player;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.asom.player.service.Album;
import com.asom.player.service.Artist;
import com.asom.player.service.MediaPlaylist;
import com.asom.player.service.Player;
import com.asom.player.service.Song;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.IntentAction;
import com.markupartist.android.widget.actionbar.R;

public class ListActivity extends CustomActivity {

	private String contentType;
	private ListView lv;
	private BaseAdapter adapter;

	protected enum ContentType {
		Songs, Playlists, Artists, Albums, Favorites, NOVALUE;

		public static ContentType toContentType(String str) {
			try {
				return valueOf(str);
			} catch (Exception ex) {
				return NOVALUE;
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.content_list_layout);

		Bundle bundle = this.getIntent().getExtras();
		contentType = bundle.getString("contentType");

		lv = (ListView) findViewById(R.id.listView_stuff);

		initializeActionBar(contentType);

		player = Player.getInstance(getContentResolver());

		switch (ContentType.toContentType(contentType)) {
		case Songs:
			adapter = new SongsListAdapter(this, player.getSongs());
			break;
		case Playlists:
			adapter = new PlaylistAdapter(this, player.getPlaylists());
			break;
		case Artists:
			adapter = new ArtistsListAdapter(this, player.getArtists());
			break;
		case Albums:
			adapter = new AlbumsListAdapter(this, player.getAlbums());
			break;
		case Favorites:
			adapter = new SongsListAdapter(this, player.getFavorites());
			break;
		default:
			Toast.makeText(this, "Something went wrong!", Toast.LENGTH_SHORT)
					.show();
		}

		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Song songToBePlayed = (Song) lv.getAdapter().getItem(position);

				player.play(songToBePlayed);

				Intent listToPlayerScreen = new Intent(ListActivity.this,
						PlayerActivity.class);

				startActivity(listToPlayerScreen);
			}

		});

		registerForContextMenu(lv);
	}

	private void initializeActionBar(String title) {

		final Action shareAction = new IntentAction(this, null,
				R.drawable.ic_title_share_default);

		if (actionBar != null) {
			actionBar.setTitle(title);
			actionBar.setHomeAction(new GoHomeAction());
			actionBar.setDisplayHomeAsUpEnabled(true);
			actionBar.addAction(shareAction);
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);

		if (contentType.equals("Songs")) {
			menu.setHeaderTitle("Choose action");
			menu.add(Menu.NONE, 0, 0, "Add to Favorites");
			menu.add(Menu.NONE, 1, 1, "Post on Facebook");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();

		Song songItem = (Song) lv.getAdapter().getItem(info.position);
		String textToPost = "Listening to " + songItem.getTitle() + " by " + songItem.getArtist();

		if (item.getTitle() == "Add to Favorites") {
			player.favorite(songItem);
		} else if (item.getTitle() == "Post on Facebook") {
//			postSong(textToPost);
		} else {
			return false;
		}
		return true;
	}

	private class SongsListAdapter extends BaseAdapter {

		private Song[] mSongs;
		private Context mContext;

		private LayoutInflater mInflater;

		public SongsListAdapter(Context context, Song[] songs) {
			mContext = context;
			mSongs = songs;
			mInflater = LayoutInflater.from(mContext);
		}

		public int getCount() {
			return mSongs.length;
		}

		public Object getItem(int position) {
			return mSongs[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.content_list_item_layout, null);

				holder = new ViewHolder();

				holder.listItemTitle = (TextView) convertView
						.findViewById(R.id.list_item_title_TextView);
				holder.listItemText = (TextView) convertView
						.findViewById(R.id.list_item_text_TextView);
				holder.listItemAlbumArtImage = (ImageView) convertView
						.findViewById(R.id.list_item_album_art_ImageView);
				holder.listItemPlayingImage = (ImageView) convertView
						.findViewById(R.id.list_item_playing_ImageView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.listItemTitle.setText(mSongs[position].getArtist());
			holder.listItemText.setText(mSongs[position].getTitle());

			if (mSongs[position].getAlbumArt() != null)
				holder.listItemAlbumArtImage.setImageURI(Uri
						.parse(mSongs[position].getAlbumArt()));
			else
				holder.listItemAlbumArtImage
						.setImageResource(R.drawable.amoeba_100);

			return convertView;
		}
	}

	private class PlaylistAdapter extends BaseAdapter {

		private MediaPlaylist[] mPlaylists;
		private Context mContext;

		private LayoutInflater mInflater;

		public PlaylistAdapter(Context context, MediaPlaylist[] playlists) {
			mContext = context;
			mPlaylists = playlists;
			mInflater = LayoutInflater.from(mContext);
		}

		public int getCount() {
			return mPlaylists.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.content_list_item_layout, null);

				holder = new ViewHolder();

				holder.listItemTitle = (TextView) convertView
						.findViewById(R.id.list_item_title_TextView);
				holder.listItemText = (TextView) convertView
						.findViewById(R.id.list_item_text_TextView);
				holder.listItemAlbumArtImage = (ImageView) convertView
						.findViewById(R.id.list_item_album_art_ImageView);
				holder.listItemPlayingImage = (ImageView) convertView
						.findViewById(R.id.list_item_playing_ImageView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.listItemTitle.setText(mPlaylists[position].getName());
			holder.listItemText
					.setText(mPlaylists[position].getSongs().length > 1 ? mPlaylists[position]
							.getSongs().length
							+ " songs"
							: "1 song");

			if (mPlaylists[position].getSongs()[0].getAlbumArt() != null)
				holder.listItemAlbumArtImage
						.setImageURI(Uri
								.parse(mPlaylists[position].getSongs()[0]
										.getAlbumArt()));
			else
				holder.listItemAlbumArtImage
						.setImageResource(R.drawable.amoeba_100);

			return convertView;
		}
	}

	private class ArtistsListAdapter extends BaseAdapter {

		private Artist[] mArtists;
		private Context mContext;

		private LayoutInflater mInflater;

		public ArtistsListAdapter(Context context, Artist[] artists) {
			mContext = context;
			mArtists = artists;
			mInflater = LayoutInflater.from(mContext);
		}

		public int getCount() {
			return mArtists.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.content_list_item_layout, null);

				holder = new ViewHolder();

				holder.listItemTitle = (TextView) convertView
						.findViewById(R.id.list_item_title_TextView);
				holder.listItemText = (TextView) convertView
						.findViewById(R.id.list_item_text_TextView);
				holder.listItemAlbumArtImage = (ImageView) convertView
						.findViewById(R.id.list_item_album_art_ImageView);
				holder.listItemPlayingImage = (ImageView) convertView
						.findViewById(R.id.list_item_playing_ImageView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.listItemTitle.setText(mArtists[position].getArtistName());
			holder.listItemText
					.setText(mArtists[position].getNrAlbums() > 1 ? mArtists[position]
							.getNrAlbums()
							+ " albums"
							: "1 album");

			if (mArtists[position].getAlbumArt() != null)
				holder.listItemAlbumArtImage.setImageURI(Uri
						.parse(mArtists[position].getAlbumArt()));
			else
				holder.listItemAlbumArtImage
						.setImageResource(R.drawable.amoeba_100);

			return convertView;
		}
	}

	private class AlbumsListAdapter extends BaseAdapter {

		private Album[] mAlbums;
		private Context mContext;

		private LayoutInflater mInflater;

		public AlbumsListAdapter(Context context, Album[] albums) {
			mContext = context;
			mAlbums = albums;
			mInflater = LayoutInflater.from(mContext);
		}

		public int getCount() {
			return mAlbums.length;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(
						R.layout.content_list_item_layout, null);

				holder = new ViewHolder();

				holder.listItemTitle = (TextView) convertView
						.findViewById(R.id.list_item_title_TextView);
				holder.listItemText = (TextView) convertView
						.findViewById(R.id.list_item_text_TextView);
				holder.listItemAlbumArtImage = (ImageView) convertView
						.findViewById(R.id.list_item_album_art_ImageView);
				holder.listItemPlayingImage = (ImageView) convertView
						.findViewById(R.id.list_item_playing_ImageView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.listItemTitle.setText(mAlbums[position].getName());
			holder.listItemText.setText(mAlbums[position].getArtist());

			if (mAlbums[position].getAlbumArt() != null)
				holder.listItemAlbumArtImage.setImageURI(Uri
						.parse(mAlbums[position].getAlbumArt()));
			else
				holder.listItemAlbumArtImage
						.setImageResource(R.drawable.amoeba_100);

			return convertView;
		}
	}

	private static class ViewHolder {

		private TextView listItemTitle;
		private TextView listItemText;

		private ImageView listItemAlbumArtImage;
		private ImageView listItemPlayingImage;
	}
}
